package com.ruoyi.project.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.sql.SqlReplaceUtil;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.api.service.*;
import com.ruoyi.project.datamanagement.domain.DataSources;
import com.ruoyi.project.datamanagement.domain.SysDataSearch;
import com.ruoyi.project.datamanagement.mapper.DataSourcesMapper;
import com.ruoyi.project.datamanagement.mapper.SysDataSearchMapper;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author anan.
 * @version 1.0
 * @create 2021-04-08 13-16
 */
@Service
public class SelectServiceImpl implements SelectService {

    @Autowired
    private SysDataSearchMapper dataSearchMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DataSourcesMapper dataSourcesMapper;

    @Autowired
    private Test39Service test39Service;

    @Autowired
    private PortalService portalService;

    @Autowired
    private DbcenterService dbcenterService;

    @Autowired
    private Human34Service human34Service;

    @Autowired
    private Kafuni53Service kafuni53Service;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取查询语句
     * @param method
     * @return
     */
    @Override
    public SysDataSearch getDataSourceByMethod(String method) {
        SysDataSearch dataSearch = new SysDataSearch();
        if (!method.isEmpty()) {
            QueryWrapper query = new QueryWrapper();
            query.eq("search_name_en", method);
            dataSearch = dataSearchMapper.selectOne(query);
        }
        return dataSearch;
    }

    @Override
    public List getData(SysDataSearch dataSearch,Map map)
    {
        /** String type=(String) map.get("sql"); */
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        boolean isAdmin = user.isAdmin();
        String type;
        if (isAdmin) {
            type = "adminSql";
        }else{
            type = "sql";
        }
        String sql="";
        if ("sql".equals(type)) {
            sql = dataSearch.getSql();
        } else if ("adminSql".equals(type)) {
            sql = dataSearch.getAdminSql();
        }
        map.remove("sql");
        map.remove("method");
        map.put("fdd", user.getUserName());
        map.put("userId", user.getUserId());
        sql = SqlReplaceUtil.getSql(sql, map);

        List list = new ArrayList();
        if (dataSearch.getDataSourceId()!=null)
        {
            DataSources dataSources = dataSourcesMapper.selectDataSourcesById(dataSearch.getDataSourceId());
            switch (dataSources.getName())
            {
                case "gmqruoyi":
                    list = jdbcTemplate.queryForList(sql);
                    break;
                case "portal":
                    list = portalService.select(sql);
                    break;
                case "test39":
                    list = test39Service.select(sql);
                    break;
                case "dbcenter":
                    list = dbcenterService.select(sql);
                    break;
                case "human34":
                    list = human34Service.select(sql);
                    break;
                case "kafuni53":
                    list = kafuni53Service.select(sql);
                    break;
            }
        }
        return list;
    }
}
